//
//  HWTakeDetailVc.m
//  JWC
//
//  Created by lanou on 16/4/18.
//  Copyright © 2016年 陈帅. All rights reserved.
//

#import "HWTakeDetailVc.h"
#import "HWRecommentCell.h"
#import "HWFrame.h"
#import "HWEssenceTool.h"
#import "HWTakeDetailCell.h"
#import "MJRefresh.h"
#import "HWCommentViewController.h"
#import "HWRecommendModel.h"

static NSString *cellID = @"new";
@interface HWTakeDetailVc ()

@property (nonatomic, strong)HWEssenceTool *tool;
@property (nonatomic, strong)NSMutableArray *modelArray;
@property (nonatomic, assign)NSInteger np;

@end

@implementation HWTakeDetailVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.modelArray = [NSMutableArray array];
    
    [self setupHeader];
    [self setupRefresh];
    [self setTableView];
    [self enterComment];
    [self newButton];
    [self hotButton];
}

#pragma mark --- 评论按钮 ---
- (void)enterComment
{
    
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"commentClick" object:nil] subscribeNext:^(NSNotification *noti) {
        HWFrame *topFrame = noti.userInfo[@"topFrame"];
        HWCommentViewController *commentVc =[[HWCommentViewController alloc]init];
        commentVc.frame = topFrame;
        [self.navigationController pushViewController:commentVc animated:YES];
    }];
}

#pragma mark --- 最新、最热 ---
- (void)newButton
{
    
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"newButton" object:nil] subscribeNext:^(id x) {
        
        [self.tool getTakeDetailDataWithID:self.takeModel.theme_id type:@"new" block:^(id json, NSInteger np) {
            [self.modelArray removeAllObjects];
            for (HWRecommendModel *model in json)
            {
                HWFrame *frame = [[HWFrame alloc]init];
                frame.model = model;
                [self.modelArray addObject:frame];
            }
            self.np = np;
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }];
        
    }];
}

- (void)hotButton
{
    
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"hotButton" object:nil] subscribeNext:^(id x) {
        
        [self.tool getTakeDetailDataWithID:self.takeModel.theme_id type:@"hot" block:^(id json, NSInteger np) {
            [self.modelArray removeAllObjects];
            for (HWRecommendModel *model in json)
            {
                HWFrame *frame = [[HWFrame alloc]init];
                frame.model = model;
                [self.modelArray addObject:frame];
            }
            self.np = np;
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }];
        
    }];
}

#pragma mark --- 设置表头 ---
- (void)setupHeader
{
    HWTakeDetailCell *cell = [HWTakeDetailCell cell];
    cell.model = self.takeModel;
    
    UIView *header = [[UIView alloc]init];
    [header addSubview:cell];
    header.frame = CGRectMake(0, 0, SCREEN_WIDTH, 210);
    self.tableView.tableHeaderView = header;
}

#pragma mark --- 下拉刷新、上拉加载 ---
- (void)setupRefresh
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewData)];
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
}

- (void)getNewData
{
    [self.tool getTakeDetailDataWithID:self.takeModel.theme_id type:@"new" block:^(id json, NSInteger np) {
        [self.modelArray removeAllObjects];

        for (HWRecommendModel *model in json)
        {
            HWFrame *frame = [[HWFrame alloc]init];
            frame.model = model;
            [self.modelArray addObject:frame];
        }
        self.np = np;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)getMoreData
{
    [self.tool getTakeDetailDataWithID:self.takeModel.theme_id type:@"new" np:self.np block:^(id json, NSInteger np) {
        for (HWRecommendModel *model in json)
        {
            HWFrame *frame = [[HWFrame alloc]init];
            frame.model = model;
            [self.modelArray addObject:frame];
        }
        self.np = np;
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark --- 设置tableView ---
- (void)setTableView
{
    [self.tableView registerNib:[UINib nibWithNibName:@"HWRecommentCell" bundle:nil] forCellReuseIdentifier:cellID];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWRecommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.topFrame = self.modelArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWFrame *topFrame = self.modelArray[indexPath.row];
    return topFrame.cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWFrame *frame = self.modelArray[indexPath.row];
    HWCommentViewController *commentVc = [[HWCommentViewController alloc]init];
    commentVc.frame = frame;
    [self.navigationController pushViewController:commentVc animated:YES];
}

// 懒加载
- (HWEssenceTool *)tool{
    if (_tool == nil) {
        _tool = [[HWEssenceTool alloc]init];
        
    }
    return _tool;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
